import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {ParamConfig} from '../common/param.config';
import 'rxjs/add/operator/toPromise';
import {AddEvent} from './logicAdd';
import {AddPlacePage} from '../place/place';
import {UserService} from '../user/user.service';
import {Router} from '@angular/router';

@Injectable()
export class LogicAddService {
  fileId: string;
  public token: string;
  private hostUrlgetPath = ParamConfig.HTTP_WEB_HOST_URL + 'path/getPathData'; // URL to web api
  private hostUrlGetMapId = ParamConfig.HTTP_WEB_HOST_URL + 'path/getPathByid'; // URL to web api
  private hostUrlgetreal = ParamConfig.HTTP_WEB_HOST_URL + 'realNode/getRealNodeData'; // 物理基点查询
  private hostUrlGetMap = ParamConfig.HTTP_WEB_HOST_URL + 'rbMap/getMapByRobotId'; // 获取地图
  private hostUrlEvent = ParamConfig.HTTP_WEB_HOST_URL + 'event/getWebEventData'; // 查询事件
  constructor(private http: Http, private userService: UserService, private router: Router) {
  }

// 查询路径
  getPath(page, map): Promise<any> {
    const request = JSON.stringify({
      rows: '10000',
      page: page,
      mapId: map
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlgetPath, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return [true, body];
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

// 查询物理基点
  getReal(page, mapId): Promise<any> {
    const request = JSON.stringify({
      mapId: mapId
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlgetreal, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return [true, body.datas];
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

  // 查询MapId
  getMapId(id): Promise<any> {
    const request = JSON.stringify({
      id: id
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlGetMapId, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return [true, body];
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

  // 机器人唯一标识获取mapId
  getMap(): Promise<any> {
    const request = JSON.stringify({
      robotId: ''
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlGetMap, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            return [true, body];
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

// 查询事件
  getEvLofic(page): Promise<any> {
    const request = JSON.stringify({
      rows: 1000,
      page: page,
    });
    const requestBody = 'json=' + request;
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    return this.http
      .post(this.hostUrlEvent, requestBody, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            const pagedata = new AddPlacePage();
            pagedata.addEvent = body as AddEvent[];
            pagedata.totalPages = body.total;
            return pagedata;
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }

  private handleError(error: any): Promise<[boolean, any]> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject([false, error.toString()]);
  }
}
